
package com.xwyzsn.rentcar.controller;
import com.xwyzsn.rentcar.entity.Car;
import com.xwyzsn.rentcar.service.CarService;
import com.xwyzsn.rentcar.utils.Result;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

@RestController()
@RequestMapping(value = "/car")
@CrossOrigin(value = "*")
public class CarController {

    @Autowired
    private CarService service;
    @PostMapping("/get_one")
    public Result getOne(@RequestBody Map<String,?> payload){
            List<Car>  cars =  service.getOne(payload);
            return Result.succ(cars);
    }


    @PostMapping("/add_one")
    public Result addOne(@RequestBody Map<String,?>payload){

        return Result.succ(service.addOne(payload));
    }

    @PostMapping("/delete_one")
    public Result deleteOne(@RequestBody Map<String,?>payload){
        return Result.succ(service.deleteOne(payload));
    }

    @PostMapping("/update_one")
    public Result updateOne(@RequestBody Map<String,?>payload){
        return Result.succ(service.updateOne(payload));
    }
    @PostMapping("/upload")
    public Result uploadPics(@RequestBody MultipartFile file) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        Path currentRelativePath = Paths.get("");
        String currentAbsolutePath = currentRelativePath.toAbsolutePath().toString();
        String tmpPath = currentAbsolutePath + "/tmp/";
        if (!new File(tmpPath).exists()){
            boolean res = new File(tmpPath).mkdir();
            if(res){
                System.out.println("创建成功");
            }else{
                System.out.println("创建失败");
            }
        }
        File newFile = new File(tmpPath + file.getOriginalFilename());
        file.transferTo(newFile);
        MinioClient minioClient =
                MinioClient.builder()
                        .endpoint("113.31.110.212",9000,false)
                        .credentials("jBEqfAG6lAUMYqhf9ZuQ", "zzh0117.")
                        .build();
        boolean found =
                minioClient.bucketExists(BucketExistsArgs.builder().bucket("car").build());
        if (!found) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket("car").build());
        }
        minioClient.uploadObject(
                UploadObjectArgs.builder()
                        .bucket("car")
                        .object(file.getOriginalFilename())
                        .filename(tmpPath + file.getOriginalFilename())
                        .build());


        return Result.succ("http://113.31.110.212:9000/car/"+file.getOriginalFilename());
    }
    @GetMapping("/hello")
    public  String hello(){
        return "hello";
    }
}
